import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:foreign_exchange_translator/screens/splash_screen.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'generated/l10n/app_localizations.dart';
import 'services/language_service.dart';
import 'services/theme_service.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化SharedPreferences
  final prefs = await SharedPreferences.getInstance();

  // 强制竖屏
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  // 设置系统UI样式
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark,
    systemNavigationBarColor: Colors.transparent,
    systemNavigationBarIconBrightness: Brightness.dark,
  ));

  runApp(MyApp(prefs: prefs));
}

class MyApp extends StatelessWidget {
  final SharedPreferences prefs;

  const MyApp({super.key, required this.prefs});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => LanguageService(prefs)),
        ChangeNotifierProvider(create: (context) => ThemeService(prefs)),
      ],
      child: Consumer2<LanguageService, ThemeService>(
        builder: (context, languageService, themeService, child) {
          // 根据当前主题模式更新系统UI样式
          SystemChrome.setSystemUIOverlayStyle(
            SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              statusBarIconBrightness:
                  themeService.isDarkMode ? Brightness.light : Brightness.dark,
              systemNavigationBarColor: Colors.transparent,
              systemNavigationBarIconBrightness:
                  themeService.isDarkMode ? Brightness.light : Brightness.dark,
              statusBarBrightness:
                  themeService.isDarkMode ? Brightness.dark : Brightness.light,
            ),
          );

          return MaterialApp(
            title: 'Foreign Exchange Translator',
            debugShowCheckedModeBanner: false,

            // 国际化支持
            localizationsDelegates: [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: LanguageService.supportedLocales,
            locale: languageService.currentLocale,

            // 应用主题
            theme: themeService.theme,
            darkTheme: themeService.theme,
            themeMode:
                themeService.isDarkMode ? ThemeMode.dark : ThemeMode.light,

            // 主页
            home: Theme(
              data: themeService.theme,
              child: const SplashScreen(),
            ),
          );
        },
      ),
    );
  }
}
